package com.boxin.ims.modules.momarketing.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.momarketing.entity.PageTemplate;
import com.boxin.ims.modules.momarketing.entity.Project;
import com.boxin.ims.modules.momarketing.service.ProjectService;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 移动营销项目Controller
 * @author Jakemanse
 * @version 2013-05-25
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/mom/project")
public class ProjectController extends BaseController {

	@Autowired
	private ProjectService projectService;
	@Autowired
	private QRCodeService qrCodeService;
	
	@ModelAttribute
	public Project get(@RequestParam(required=false) Long id) {
		if (id != null){
			return projectService.get(id);
		}else{
			return new Project();
		}
	}
	
	@RequiresPermissions("momarketing:project:view")
	@RequestMapping(value = {"list", ""})
	public String list(Project project, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			project.setUser(user);
		}
        Page<Project> page = projectService.find(new Page<Project>(request, response), project); 
        model.addAttribute("page", page);
		return "modules/momarketing/projectList";
	}

	@RequiresPermissions("momarketing:project:view")
	@RequestMapping(value = "form")
	public String form(Project project, Model model) {
		
		//如果非管理员，只能建一个站点
		User user = UserUtils.getUser();
		if(!user.isAdmin()){
			Project temp = projectService.getByUserId(user.getId());
			if(temp != null){
				project = temp;
			}else{
				project.setUser(user);
			}
		}
		
		model.addAttribute("project", project);
		return "modules/momarketing/projectForm";
	}

	@RequiresPermissions("momarketing:project:edit")
	@RequestMapping(value = "save")
	public String save(Project project, Model model, RedirectAttributes redirectAttributes) {
		
		project.setUser(UserUtils.getUser());
		PageTemplate pTemplate = new PageTemplate();
		pTemplate.setId(1l);
		project.setPageTemplate(pTemplate);
		if (!beanValidator(model, project)){
			return form(project, model);
		}
		projectService.save(project);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.ADMIN_PATH+"/mom/project?repage";
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-27  下午10:50:33
	 * @function <p> 保存轮播图片 </p>
	 * @param project
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("momarketing:project:edit")
	@RequestMapping(value = "savePics")
	public String savePics(@RequestParam("picFile1") MultipartFile picFile1,
			@RequestParam("picFile2") MultipartFile picFile2,
			@RequestParam("picFile3") MultipartFile picFile3,
			@RequestParam("picFile4") MultipartFile picFile4,
			HttpServletRequest request,
			Project project, Model model, RedirectAttributes redirectAttributes) {
		
		//2
		if(picFile1 != null && picFile1.getOriginalFilename().length()>4){
			String picFilePath = WechatUtils.getProjectResourceSavePath(request)+File.separator;
			FileUtils.createDirectory(picFilePath);
			String sname = picFile1.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_project_1"+fileType;
			File file = new File(picFilePath+fname);
			System.out.println("保存文件:"+picFilePath+fname);
			try {
				picFile1.transferTo(file);
				project.setPic1(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+picFilePath+fname);
				e.printStackTrace();
			}
			
		}
		//2
		if(picFile2 != null && picFile2.getOriginalFilename().length()>4){
			String picFilePath = WechatUtils.getProjectResourceSavePath(request)+File.separator;
			FileUtils.createDirectory(picFilePath);
			String sname = picFile2.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_project_2"+fileType;
			File file = new File(picFilePath+fname);
			System.out.println("保存文件:"+picFilePath+fname);
			try {
				picFile2.transferTo(file);
				project.setPic2(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+picFilePath+fname);
				e.printStackTrace();
			}
			
		}
		
		//3
		if(picFile3 != null && picFile3.getOriginalFilename().length()>4){
			String picFilePath = WechatUtils.getProjectResourceSavePath(request)+File.separator;
			FileUtils.createDirectory(picFilePath);
			String sname = picFile3.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_project_3"+fileType;
			File file = new File(picFilePath+fname);
			System.out.println("保存文件:"+picFilePath+fname);
			try {
				picFile3.transferTo(file);
				project.setPic3(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+picFilePath+fname);
				e.printStackTrace();
			}
			
		}
		
		//4
		if(picFile4 != null && picFile4.getOriginalFilename().length()>4){
			String picFilePath = WechatUtils.getProjectResourceSavePath(request)+File.separator;
			FileUtils.createDirectory(picFilePath);
			String sname = picFile4.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_project_4"+fileType;
			File file = new File(picFilePath+fname);
			System.out.println("保存文件:"+picFilePath+fname);
			try {
				picFile4.transferTo(file);
				project.setPic4(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+picFilePath+fname);
				e.printStackTrace();
			}
			
		}
		
		projectService.save(project);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.ADMIN_PATH+"/mom/project?repage";
	}
	
	
	@RequiresPermissions("momarketing:project:view")
	@RequestMapping(value = "picsForm")
	public String picicsForm(Project project, Model model, RedirectAttributes redirectAttributes) {
		
		User user = UserUtils.getUser();
		project = projectService.getByUserId(user.getId());
		//未创建微网站
		if(project == null || project.getId() <=0){
			addMessage(redirectAttributes, "请创建微网站");
			return form(project,model);
		}
		
		model.addAttribute("project", project);
		
		return  "modules/momarketing/projectPicsForm";
	}
	
	
	
	@RequiresPermissions("momarketing:project:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		projectService.delete(id);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.ADMIN_PATH+"/mom/project/?repage";
	}

}
